package com.mingzhi.ai.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatClientController {

    @Resource
    private ChatClient chatClient;

    @GetMapping("/call")
    public String generation(String userInput) {
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
                //调用大模型
                .call()
                //响应
                .content();
    }


    //JDK16 特性创建实体类
    //创建一个不可变的数据类
    record Recipe(String name, List< String>  ingredients){

    }

    @GetMapping("/call/recipe")
    public String generationRecipe(String userInput) {
        Recipe entity = chatClient.prompt()
                .user(String.format("请帮我生成%s的食谱，详细点", userInput))
                .call()
                //使用.entity(Recipe.class)方法将大模型的文本响应自动解析为Java对象
                .entity(Recipe.class);
        return entity.toString();
    }

    //流式响应
    @GetMapping(value = "/stream",produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userInput) {
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
                //调用大模型
                .stream()
                //响应
                .content();
    }
}
